//定义字典，字典是无序的

//常量,常量不能做任何修改，赋值，修改，删除都不行，这点不同于java和kotlin
let dict1 = ["key1":888,"key2":999]
let dict2 : [String:Int] = ["key1":888,"key2":999]

//变量
var dict3 : [String:Int] = [:]
var dict4 = ["key1":888,"key2":999]
var dict5 = [String:Int]()
var dict6 = Dictionary<String, Int>()

//赋值
dict3 = ["key0":111,"key1":222]
print(dict3)

//添加或修改
dict3.updateValue(777,forKey:"key0")
print(dict3)

//取值,value是可选值
let value = dict5["key1"]
let keys = dict5.keys.sorted()
let values = dict5.values.sorted()

if let value = value {
    print("value is \(value)")
}
print("keys is \(keys)")
print("values is \(values)")

//删除元素
dict3.removeValue(forKey:"key0")
print(dict3)
dict3.removeAll()
print(dict3)
//通过索引删除元素,这个索引不是int，它是一个对象，要通过方法获取
print()
var dict7 = ["key1":1,"key2":2,"key3":3]
let index = dict7.index(dict7.startIndex,offsetBy:0)
print("index is \(index)")
dict7.remove(at:index)
print("delete from index \(dict7)")
print()

//遍历字典
for item in dict4 {
    print("key is \(item.key),value is \(item.value)")
}

for (key,value) in dict4 {
    print("key is \(key),value is \(value)")
}

for sequence in dict4.enumerated(){
    print("\(sequence.offset) --> \(sequence.element.key) --> \(sequence.element.value)")
}

for (offset,item) in dict4.enumerated(){
    print("\(offset) --> \(item.key) --> \(item.value)")
}

//count 和 isEmpty 属性
print()
print("dict7 count is \(dict7.count)")
print("dict7 isEmpty=\(dict7.isEmpty)")





















